/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.jadex.customproject.wizards;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.IStructuredSelection;
import es.upm.dit.gsi.jadex.customproject.natures.JadexProjectNature;
import org.maven.ide.eclipse.core.IMavenConstants;




/**
 * Class that support the creation of a new Jadex project.
 * 
 * It creates a basic java project and adds the Jadex libraries to the classpath.
 * 
 * @author Pablo Muñoz
 */
public class JadexProjectSupport {
	
	public static IProject createProject(final String projectName, IPath location, IStructuredSelection selection) {
		if(projectName == null) return null;
		if(!(projectName.trim().length() > 0)) return null;
		try{
		final IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
		if(!newProject.exists()){			
			//STEP 1 - Create new IProject
			newProject.create(null);
			newProject.open(null);
			
			//STEP 2 - Create folder structure
			createFolder(newProject, "src/main/java/", null);
			createFolder(newProject, "src/main/resources", null);
			createFolder(newProject, "src/test/java", null);
			createFolder(newProject, "src/test/resources", null);
			createFolder(newProject, "target/classes", null);
			createFolder(newProject, "target/test-classes", null);
			
			//STEP 3 - Create custom POM.xml file			
			final IFile file = newProject.getFile(new Path(IMavenConstants.POM_FILE_NAME));
			
			try {
				InputStream stream = openContentStream("a","b","c","d");
				if (file.exists()) {
					file.setContents(stream, true, true, null);
				} else {
					file.create(stream, true, null);
				}
				stream.close();
			}
			catch(Exception e1){
				e1.printStackTrace();
			}
			
			//STEP 4 - Add MAVEN and JADEX Project natures. Update Maven Project
			
			IProjectDescription description = newProject.getDescription();
			description.setNatureIds(new String[] { JadexProjectNature.NATURE_ID,JavaCore.NATURE_ID, IMavenConstants.NATURE_ID});
			newProject.setDescription(description, null);			
		}
			return newProject;
		}
		catch(Exception e1){
			e1.printStackTrace();
		}		
		return null;
	}
		
		/*
		 * This class generates the basic structure of the POM file 
		 */
		private static InputStream openContentStream(String groupID, String artifactID, String version, String name) {			  
			String contents = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + 
					  "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"> \n" +
					  "<modelVersion>4.0.0</modelVersion> \n" + 
					  "<groupId>" + groupID + "</groupId> \n" +
					  "<artifactId>" + artifactID + "</artifactId> \n" + 
					  "<version>" + version + "</version> \n" +
					  "<packaging>jar</packaging> \n" +
					  "<name>" + name + "</name>" + 
					  "<url>http://maven.apache.org</url> \n" + 
					  "<properties> \n" +
					  "  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> \n" +
					  "</properties> \n" +
					  "<build> \n" +
					  "	<plugins> \n" +
					  "		<plugin> \n" +
					  "			<groupId>org.apache.maven.plugins</groupId> \n" +
					  "			<artifactId>maven-compiler-plugin</artifactId>\n" +
					  "			<version>2.3.2</version>\n" +
					  "		</plugin>\n" +
					  "		<plugin>\n" +
					  "			<groupId>org.apache.maven.plugins</groupId>\n" +
					  "			<artifactId>maven-source-plugin</artifactId>\n" +
					  "			<version>2.1.2</version>\n" +
					  "		</plugin>\n" +
					  "		<plugin>\n" + 
					  "			<groupId>org.apache.maven.plugins</groupId>\n" +
					  "			<artifactId>maven-javadoc-plugin</artifactId>\n" +
					  "			<version>2.7</version>\n" +
					  "		</plugin>\n" +
					  "		<plugin>\n" +
					  "			<groupId>org.apache.maven.plugins</groupId>\n" +
					  "			<artifactId>maven-jar-plugin</artifactId>\n" +
					  "			<version>2.3.1</version>\n" +
					  "		</plugin>\n" +
					  "		<plugin>\n" +
					  "			<groupId>org.apache.maven.plugins</groupId>\n" +
					  "			<artifactId>maven-dependency-plugin</artifactId>\n" +
					  "			<version>2.1</version>\n" +
					  "		</plugin>\n" +
					  "	</plugins>\n" +
					  "</build>\n" +
					  "<dependencies>\n" +
					  "  <dependency>\n" +
					  "    <groupId>junit</groupId>\n" +
					  "    <artifactId>junit</artifactId>\n" +
					  "    <version>3.8.1</version>\n" +
					  "    <scope>test</scope>\n" +
					  "  </dependency>\n" +
					  "  <dependency>\n" +
					  "  	<groupId>net.sourceforge.jadex</groupId>\n" +
					  "  	<artifactId>jadex-platform-standalone</artifactId>\n" +
					  " 	<version>2.0-rc6</version>\n" +
					  "  	<type>jar</type>\n" +
					  "  	<scope>compile</scope>\n" +
					  "  </dependency>\n" +
					  "  <dependency>\n" +
					  "  	<groupId>net.sourceforge.jadex</groupId>\n" +
					  "  	<artifactId>jadex-runtimetools</artifactId>\n" +
					  "  	<version>2.0-rc6</version>\n" +
					  "  	<type>jar</type>\n" +
					  "  	<scope>compile</scope>\n" +
					  "  </dependency>\n" +
					  "  <dependency>\n" +
					  "  	<groupId>net.sourceforge.jadex</groupId>\n" +
					  "  	<artifactId>jadex-kernel-micro</artifactId>\n" +
					  "  	<version>2.0-rc6</version>\n" +
					  "  	<type>jar</type>\n" +
					  "  	<scope>compile</scope>\n" +
					  "  </dependency>\n" +
					  "  <dependency>\n" +
					  "  	<groupId>net.sourceforge.jadex</groupId>\n" +
					  "  	<artifactId>jadex-kernel-bdibpmn</artifactId>\n" +
					  "  	<version>2.0-rc6</version>\n" +
					  "  	<type>jar</type>\n" +
					  "  	<scope>compile</scope>\n" +
					  "  </dependency>\n" +
					  "</dependencies>\n" +
					"</project>";
			return new ByteArrayInputStream(contents.getBytes());
		}
		
		/*
		 * Private method for creating a new package for the agent
		 */
		private static void createFolder(IProject project, String packageName, IProgressMonitor monitor) throws CoreException{
			String[] fragments = packageName.split("/");
			IPath path = new Path("/");
			for(String f : fragments){
				path = path.append(f + "/");
				IFolder folder = project.getFolder(path);
				if(!folder.exists()){
					folder.create(false, true, monitor);
				}			
		
			}
		}
				
}